package com.hyk.commonLib.common.utils;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by 贺玉琨 on 2019/6/5.
 */
public class KeyBoardManager {
    /**
     * 切换软键盘的状态
     * 如当前为收起变为弹出,若当前为弹出变为收起
     */
    public static void toggleKeyboard(EditText edittext) {
        InputMethodManager inputMethodManager = (InputMethodManager)
                edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 强制隐藏输入法键盘
     */
    public static void hideKeyboard(View view) {
        if (view == null) return;
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 通过定时器强制隐藏虚拟键盘
     */
    public static void hideKeyboard(View view, int delayMillis) {
        AppUtils.runOnUIDelayed(() -> hideKeyboard(view), delayMillis);
    }

    /**
     * 强制显示输入法键盘
     */
    public static void showKeyboard(EditText editText) {
        if (editText == null) return;
        InputMethodManager inputMethodManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 强制显示输入法键盘
     */
    public static void showKeyboard(EditText edittext, int delayMillis) {
        AppUtils.runOnUIDelayed(() -> showKeyboard(edittext), delayMillis);
    }

    /**
     * 输入法是否显示
     */
    public static boolean isKeyboard(EditText edittext) {
        boolean bool = false;
        InputMethodManager inputMethodManager = (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            bool = true;
        }
        return bool;
    }
}